package com.test.shiver.controller;

import com.test.shiver.entity.User;
import com.test.shiver.service.UserService;
import com.test.shiver.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserDetailServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userId = request.getParameter("id");
        User user = userService.getUserById(Integer.parseInt(userId));
        String msg;
        if(user != null){
            msg = "操作成功";
        }else{
            msg = "操作失败";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/userList").forward(request,response);
            return;
        }
        request.setAttribute("user",user);
        request.getRequestDispatcher("/userDetail.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }
}
